/* Predicates versus Function Objects */
#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

/* A predicate should always be stateless */
class Nth {		// function object that returns true for the nth call
private:
	int nth;	// call for which to return true
	int count;	// call counter
public:
	Nth(int n) : nth(n), count(0) {
	}
	bool operator() (int n) {		// should be const member func
		return ++count == nth;
	}
};

int main()
{
	list<int> coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	for (auto i : coll)
	{
		cout << i << " ";
	}
	cout << endl;

	list<int>::iterator pos;
	pos = remove_if(coll.begin(), coll.end(), Nth(3));
	// 1 2 4 5 7 8 9 10
	/* This happends because the usual implementation of
	 * the algorithm copies the predicate internally during
	 * the remove_if algorithm
	 */
	coll.erase(pos, coll.end());	
	for (auto i : coll)
	{
		cout << i << " ";
	}
	cout << endl;

	return 0;
}